<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Model\BotsModel;
use App\Model\OutTradeModel;
use App\Service\BotsService;
use App\Service\SteamService;
use Exception;

class FetchTrade extends Command
{
	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $signature = 'fetch:trade';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = '获取报价配置.';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * 获取创建中报价
	 * @return array
	 */
	protected function getOutTradeByCreating()
	{
		return OutTradeModel::getOutTradeByCreating();
	}

	/**
	 * 获取机器人信息
	 * @param  int $botId 机器人ID
	 * @return array
	 */
	protected function getBotsInfoBybotId($botId)
	{
		return BotsModel::getBotsInfoBybotId($botId);
	}

	/**
	 * 获取报价信息
	 * @param  int $tradeofferId 报价ID
	 * @return array
	 */
	protected function getOutTradeByTradeId($tradeOfferId)
	{
		return OutTradeModel::getOutTradeByTradeId($tradeOfferId);
	}

	/**
	 * 修改报价信息
	 * @param  int $tradeofferId 报价ID
	 * @param  string $confId
	 * @param  string $confKey
	 * @return bool
	 */
	protected function updateOutTradeByTradeId($tradeOfferId, $confId, $confKey)
	{
		return OutTradeModel::updateOutTradeByTradeId($tradeOfferId, $confId, $confKey);
	}

	/**
	 * 多报价处理
	 * @return bool
	 */
	protected function fetchTradeAll()
	{
		$trades = $this->getOutTradeByCreating();
		if (!$trades) {
			echo 'query reading out trade failed'."\r\n";
			return false;
		}

		foreach ($trades as $trade) {
			$this->fetchTrade($trade['bot_id'], $trade);
		}

		return true;
	}

	/**
	 * 单报价处理
	 * @return bool
	 */
	protected function fetchTrade($botId, $trade)
	{
		$info = $this->getBotsInfoBybotId($botId);
		if (!$info) {
			echo 'query bots failed, botid: '.$botId."\r\n";
			return false;
		}

		$apiKey = $info['apikey'];
		$account = $info['steam_account'];

		$steam = BotsService::getInstance()->getLgoinObjByBotId($botId, $account);
		if (!$steam) {
            echo 'query bot object failed, bot_id: '.$botId."\r\n";
			return false;
		}

		try {
			$confirmations = $steam->mobileAuth()->confirmations()->fetchConfirmations();
			if (empty($confirmations)) {
				throw new Exception('get confirmations no respond, bot_id:'.$botId);
			}

			foreach ($confirmations as $confirmation) {
				$tradeoffer_id = $confirmation->getConfirmationOfferId();
				$conf_id = $confirmation->getConfirmationId();
				$conf_key = $confirmation->getConfirmationKey();

				$checkTradeOfferId = $this->getOutTradeByTradeId($tradeoffer_id);
				if (!$checkTradeOfferId) {
					SteamService::getInstance()->cancelTradeById($apiKey, $tradeoffer_id);
					continue;
				}

				$res = $this->updateOutTradeByTradeId($tradeoffer_id, $conf_id, $conf_key);
				if (!$res) {
					echo 'update out trade failed, tradeoffer_id:'.$tradeoffer_id."\r\n";
					continue;
				}
				echo 'update out trade success'."\r\n";
			}

			return true;
		} catch (Exception $e) {
			echo $e->getMessage()."\r\n";
			return false;
		}
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle()
	{
		$this->fetchTradeAll();
		echo '数据查询完毕!'."\r\n";
		return false;
	}
}